/**
 * @Author:卿雨欣
 * @Date:2020/7/14
 * @Description:这是产品调整比例页面的现组合成分产品情况
 */
import React,{Component} from "react";
import {Card, List, WhiteSpace, InputItem, Modal, Button} from 'antd-mobile';
import axios from "axios";
import {message} from "antd";
import {getCookieId} from "../../Login/Component_Login/GetCookie";
const Item = List.Item;
const alert = Modal.alert; //弹窗警告
var num = "";
//生成提交数组
function newItem(productCode,shareRadio){
    this.productCode = productCode;
    this.shareRadio = shareRadio;
}
export default  class FormNow extends Component{
    constructor(props) {
        super(props);
        this.state={
            positionInfoDtoArrayList:[],
            productCode:"",
            shareRadio:"",
            productRatioDtoList:[],
        }
    }
    componentWillMount() {
        axios({
            method:"GET",
            url:"http://39.101.141.248:9527/transaction/getWealthPositionInfoList",
            params:{
                wealthAccountId:this.props.wealthAccountId,
                userId:getCookieId(),
            },
        }).then(res=>{
            if(res.data.code === 200){
                this.setState({
                    positionInfoDtoArrayList:res.data.positionInfoDtoArrayList,
                })
            }
        }).catch(error=>{
            console.log(error);
        });
    }
    onSubmit(){
        let total=0;
        var productArray = []
        this.props.form.validateFields((error,values) => {
            if (!error) {
              // positionInfoDtoArrayList
                let items = this.state.positionInfoDtoArrayList;
                for(let i=0;i<items.length;i++){
                    let name = items[i].proName;
                    let ratio = Number(values[name])*100/10000;
                    total += ratio;
                    let productObj = new newItem(items[i].proTsCode,ratio);
                    productArray.push(productObj);
                }
            // if(total !== 1){
             if(false){
                alert(
                    "比例提示",
                    "比例相加必须为100%",
                    [
                        {text:'确定',onPress:()=>console.log('确定')},
                    ])
            }else{
                axios({
                    method:"POST",
                    url:"http://39.101.141.248:9527/transaction/adjustmentShare",
                    data:{
                        comBaseId:this.props.comBaseId,
                        userId:getCookieId(),
                        productRatioDtoList:productArray,
                    },
                }).then(res=>{
                    var data =res.data;
                    var path = {
                        pathname:'/changeRatioResult',
                        query:data,
                    }
                    if(res.data.code === 200){
                        alert(
                            "结果提示",
                            "调整比例成功",
                            [
                                {text:'确定',onPress:()=>this.props.history.push(path)},
                            ]
                        )}
                }).catch(error=>{
                    console.log(error);
                });
            }
            }else{
                alert(
                    "输入提示",
                    "请输入符合条件的比例",
                    [
                        {text:'确定',onPress:()=>console.log('确定')},
                    ])
            }
        })
        this.setState({
            productRatioDtoList:productArray,
        })
    }
    //表单清空
    onReset = () => {
        this.props.form.resetFields();
    }
    render() {
        const { getFieldProps, getFieldError } = this.props.form;
        return(
            <form>
            <div>
                <Card full>
                    <Card.Header
                        title={
                            <span style={{margin:"0 auto"}}>{this.props.comBaseName}</span>
                        }
                    />
                    <Card.Body>
                        <div style={{ display: '', alignItems: 'center', justifyContent: 'center',marginTop:-15, backgroundColor: '#fff' }}>
                            <List >
                                {
                                    this.state.positionInfoDtoArrayList.map((item,index)=>{
                                        return(
                                            <Item  wrap key={item.proTsCode}>
                                                <div className="font3" style={{float:"left",marginTop:-20,height:40}}>
                                                    <p style={{marginTop:10,fontSize:16}}>{item.proName}</p>
                                                    <p className="gray-text" style={{textAlign:"left"}}>{item.proTsCode}</p>
                                                </div>
                                                <div className="font3" style={{float:"right",marginTop:-20,height:40}}>
                                                    <p style={{marginTop:10,fontSize:16,color:"red"}}>{item.amount}</p>
                                                    <p className="gray-text" style={{textAlign:"right"}}>{item.ratio}</p>
                                                </div>
                                            </Item>
                                        )
                                    })
                                }
                            </List>
                        </div>
                    </Card.Body>
                </Card>
                <List renderHeader={() => '调整比例输入栏'}>
                    {
                        this.state.positionInfoDtoArrayList.map((item, index) => {
                            /*let name = item.proTsCode;*/
                            num +=1;
                            return (
                                <InputItem key={item.proTsCode}
                                    {...getFieldProps(item.proName, {
                                        // 输入小数点后最多两位
                                        normalize: (v, prev) => {
                                            if (v && !/^(([1-9]\d{0,1})|0)(\.\d{0,2}?)?$/.test(v)) {
                                                if (v === '.') {
                                                    return '0.';
                                                }
                                                return prev;
                                            }
                                            return v;
                                        },
                                        rules: [
                                            { required: true, message: '比例不能为空' },
                                        ],
                                    })}
                                    extra="%"
                                    clear
                                    type="digit"
                                    error={!!getFieldError('account')}
                                    onErrorClick={() => {
                                        alert(getFieldError('account').join('、'));
                                    }}
                                >{item.proName}:</InputItem>
                            )
                        })
                    }
                </List>
            </div>
                <WhiteSpace size="lg"/>
                <Button onClick={this.onSubmit.bind(this)} style={{width:"90%",backgroundColor:"#f8a83f",color:"white",margin:"0 auto"}}>确定</Button>
            </form>
        )
    }
}
